﻿namespace Yom.WebService.Service
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Xml;
    using Yom.Common;

    public class GetRss
    {
        public void OutWrite()
        {
            HttpResponse Response = HttpContext.Current.Response;
            // 创建XML对象
            XmlDocument xmlDoc = new XmlDocument();
            // 设置基本信息
            XmlDeclaration nodeDeclar = xmlDoc.CreateXmlDeclaration("1.0", Encoding.UTF8.BodyName, "yes");
            xmlDoc.AppendChild(nodeDeclar);
            // 创建RSS基本信息
            XmlElement root = xmlDoc.CreateElement("rss");
            root.SetAttribute("version", "2.0");
            xmlDoc.AppendChild(root);
            // 创建channel节点
            XmlElement channel = xmlDoc.CreateElement("channel");
            root.AppendChild(channel);
            // 创建title节点
            XmlElement element = xmlDoc.CreateElement("title");
            XmlNode node = xmlDoc.CreateTextNode(SiteLoad.Scheme.SiteTitle);
            element.AppendChild(node);
            channel.AppendChild(element);
            // 创建link节点
            element = xmlDoc.CreateElement("link");
            node = xmlDoc.CreateTextNode(SiteLoad.Scheme.SiteUrl + "/");
            element.AppendChild(node);
            channel.AppendChild(element);
            // 创建language节点
            element = xmlDoc.CreateElement("language");
            node = xmlDoc.CreateTextNode("zh-CN");
            element.AppendChild(node);
            channel.AppendChild(element);
            // 创建copyright节点
            element = xmlDoc.CreateElement("copyright");
            node = xmlDoc.CreateTextNode(SiteLoad.Scheme.CopyRight);
            element.AppendChild(node);
            channel.AppendChild(element);
            // 创建description节点
            element = xmlDoc.CreateElement("description");
            node = xmlDoc.CreateTextNode(SiteLoad.Scheme.Description);
            element.AppendChild(node);
            channel.AppendChild(element);
            // 创建image节点
            XmlElement imgElement = xmlDoc.CreateElement("image");
            channel.AppendChild(imgElement);
            // 创建image下的title节点
            element = xmlDoc.CreateElement("title");
            node = xmlDoc.CreateTextNode(SiteLoad.Scheme.SiteTitle);
            element.AppendChild(node);
            imgElement.AppendChild(element);
            // 创建image下的url节点
            element = xmlDoc.CreateElement("url");
            node = xmlDoc.CreateTextNode(SiteLoad.Scheme.SiteUrl + "/Images/Common/Logo.jpg");
            element.AppendChild(node);
            imgElement.AppendChild(element);
            // 创建image下的link节点
            element = xmlDoc.CreateElement("link");
            node = xmlDoc.CreateTextNode(SiteLoad.Scheme.SiteUrl + "/");
            element.AppendChild(node);
            imgElement.AppendChild(element);

            string CID = Factory.Request("CID");

            if (string.IsNullOrEmpty(CID))
            {
                List<Yom.Model.Comic> list = new Yom.Data.Comic().GetList(15, 1, "", false);
                foreach (Yom.Model.Comic model in list)
                {
                    // 创建item节点
                    XmlElement aItem = xmlDoc.CreateElement("item");
                    channel.AppendChild(aItem);
                    // 创建link节点
                    element = xmlDoc.CreateElement("link");
                    node = xmlDoc.CreateTextNode(SiteLoad.Scheme.SiteUrl + "/Cartoon/" + new Yom.Data.Category().GetModel(model.CategoryID).SaveName + "/" + Math.Ceiling(Convert.ToDouble(model.ComicID) / 1000).ToString() + "/" + model.ComicID.ToString() + "/Index." + SiteLoad.Scheme.Ext);
                    element.AppendChild(node);
                    aItem.AppendChild(element);
                    // 创建title节点
                    element = xmlDoc.CreateElement("title");
                    node = xmlDoc.CreateTextNode(model.ComicName);
                    element.AppendChild(node);
                    aItem.AppendChild(element);
                    // 创建author节点
                    element = xmlDoc.CreateElement("author");
                    node = xmlDoc.CreateTextNode(model.Author);
                    element.AppendChild(node);
                    aItem.AppendChild(element);
                    // 创建category节点
                    element = xmlDoc.CreateElement("category");
                    node = xmlDoc.CreateTextNode(new Yom.Data.Category().GetModel(model.CategoryID).CategoryName);
                    element.AppendChild(node);
                    aItem.AppendChild(element);
                    // 创建pubDate节点
                    element = xmlDoc.CreateElement("pubDate");
                    node = xmlDoc.CreateTextNode(model.LastTime.ToString("r"));
                    element.AppendChild(node);
                    aItem.AppendChild(element);
                    // 创建description节点
                    element = xmlDoc.CreateElement("description");
                    node = xmlDoc.CreateTextNode(model.Intro);
                    element.AppendChild(node);
                    aItem.AppendChild(element);
                }
            }
            else
            {
                Yom.Model.Category category = new Yom.Data.Category().GetModel(CID.ToInt());
                List<Yom.Model.Comic> clist = new Yom.Data.Comic().GetList(15, 1, "[CategoryID]=" + CID, false);
                foreach (Yom.Model.Comic cmodel in clist)
                {
                    // 创建item节点
                    XmlElement cItem = xmlDoc.CreateElement("item");
                    channel.AppendChild(cItem);
                    // 创建link节点
                    element = xmlDoc.CreateElement("link");
                    node = xmlDoc.CreateTextNode(SiteLoad.Scheme.SiteUrl + "/Cartoon/" + new Yom.Data.Category().GetModel(cmodel.CategoryID).SaveName + "/" + Math.Ceiling(Convert.ToDouble(cmodel.ComicID) / 1000).ToString() + "/" + cmodel.ComicID.ToString() + "/Index." + SiteLoad.Scheme.Ext);
                    element.AppendChild(node);
                    cItem.AppendChild(element);
                    // 创建title节点
                    element = xmlDoc.CreateElement("title");
                    node = xmlDoc.CreateTextNode(cmodel.ComicName);
                    element.AppendChild(node);
                    cItem.AppendChild(element);
                    // 创建author节点
                    element = xmlDoc.CreateElement("author");
                    node = xmlDoc.CreateTextNode(cmodel.Author);
                    element.AppendChild(node);
                    cItem.AppendChild(element);
                    // 创建category节点
                    element = xmlDoc.CreateElement("category");
                    node = xmlDoc.CreateTextNode(new Yom.Data.Category().GetModel(cmodel.CategoryID).CategoryName);
                    element.AppendChild(node);
                    cItem.AppendChild(element);
                    // 创建pubDate节点
                    element = xmlDoc.CreateElement("pubDate");
                    node = xmlDoc.CreateTextNode(cmodel.LastTime.ToString("r"));
                    element.AppendChild(node);
                    cItem.AppendChild(element);
                    // 创建description节点
                    element = xmlDoc.CreateElement("description");
                    node = xmlDoc.CreateTextNode(cmodel.Intro);
                    element.AppendChild(node);
                    cItem.AppendChild(element);
                }
            }

            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;
            xmlDoc.WriteTo(writer);
            writer.Flush();
            writer.Close();
        }
    }
}